﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class editForum : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Btn_create.Visible = false;
        Btn_edit.Visible = false;

        CGeneral.CheckLogin(Request, Session);
        //权限验证
        Permission per = new Permission(int.Parse(Session["gid"].ToString()));
        if (per.awmag == 0)
            CGeneral.GoToErrorPage(Response, "custom", "YOU NEED MORE POWER");
        
        
        if (Request.QueryString["type"]==null)
        {
            CGeneral.GoToErrorPage(Response, "ParamWrong");
        }
        string type = Request.QueryString["type"];

        if (type == "create")
        {
            fengmian.Visible = false;

            if (IsPostBack)
            {
                //begin 创建版块
                CForum cf = new CForum();
                cf.m_name = this.editforum_name.Value;
                cf.m_description = this.editforum_description.Value;
                cf.isVisible = 1;
                cf.Publish();

                //end

                //拷贝新版块 封面图
                System.IO.File.Copy(Server.MapPath("avatar")+"/noavatar_small.png",Server.MapPath("avatar")+"/forum_"+cf.m_fid+".png");

                Response.Redirect("editForum.aspx?type=edit&fid="+cf.m_fid);
            }
            
            Btn_create.Visible = true;

        }
        else if (type=="edit")
        {
            if (Request.QueryString["fid"] == null)
                CGeneral.GoToErrorPage(Response, "ParamWrong");

            int m_fid = -1;
            CForum cf;
            try
            {
                m_fid = int.Parse(Request.QueryString["fid"].ToString());    
            }
            catch
            {
                CGeneral.GoToErrorPage(Response, "ParamWrong");
            }
            cf = new CForum(m_fid);
            if (IsPostBack)
            {
                //begin 保存版块

                if (FileUpload_img.HasFile)
                {
                    string extend = System.IO.Path.GetExtension(FileUpload_img.FileName);
                    if (extend == ".jpg" || extend == ".bmp" || extend == ".png" || extend == ".gif")
                    {
                        string filename = Server.MapPath("avatar") + "\\fornum__" + cf.m_fid + extend;
                        FileUpload_img.SaveAs(filename);
                        cf.UpdateAvatar(filename);
                        System.IO.File.Delete(filename);
                        Response.Redirect(Request.UrlReferrer.ToString());
                    }
                }

                cf.m_name = editforum_name.Value;
                cf.m_description = editforum_description.Value;
                cf.Update();
                //end
            }

            //begin 读取版块信息然后显示
            this.editforum_img.ImageUrl = "avatar/forum_" + cf.m_fid + ".png";
            this.editforum_description.Value = cf.m_description;
            this.editforum_name.Value = cf.m_name;
            //end
            Btn_edit.Visible = true;
        }



    }
}